package bomb.logic;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

import bomb.Game;
import bomb.utils.Highscores;
import bomb.utils.Letras;
import bomb.utils.BlockedTest;


public class Level extends BasicGameState {
    

    protected int numero = 0;
    TiledMap map = null;
    Image barra = null;
    protected static float timer = 0;
    protected static boolean  terminado = false;
    protected static boolean  paused = false;
    boolean[][] blocked = null;
    protected Personagem hero = null;
    protected Bomba[] bombs = null;
    protected Lugar[] places = null;
    
 
    
    protected int ident = 0;
    public Level(int id,int numero) {
        this.ident = id;
        this.numero = numero;
        bombs = new Bomba[numero];
        places = new Lugar[numero];
    }
    
    @Override
    public int getID() {
        return ident;
    }
    
    
    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        map = new TiledMap("src/maps/map"+ Integer.toString(this.ident + 1) +".tmx","src/maps");
        barra = new Image("src/images/barra.png");
        blocked = new boolean[10][10];
        BlockedTest.mapear(blocked, map);
        
    }
 

 
    @SuppressWarnings("deprecation")
	@Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        map.render(0,0);
        //desenho dos lugares das bombas.
        for(int k=0; k<numero; k++){
            places[k].getImg().draw(places[k].getX(), places[k].getY(),1.0f);
        }
        //desenho das bombas
        for(int k=0; k<numero; k++){
            bombs[k].getImg().draw(bombs[k].getX(), bombs[k].getY(), 1.0f);
        }
        //desenho do heroi.
        hero.getImg().draw(hero.getX()+4, hero.getY()-14, 1.2f);
        //desenho da barra superior
        barra.draw(0,0);
        Letras.getLetra2().drawString(5, 3, "Nível "+Integer.toString(this.ident+1), Color.black);
        Letras.getLetra1().drawString(280, 3, "Melhor: "+Integer.toString(Highscores.getScore(this.ident, 0)), Color.black);
        Letras.getLetra1().drawString(292, 21, "Atual: "+Integer.toString(hero.getMovs()), Color.black);
        
    }
    

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException {
    	if(!Game.isPaused()){
	        Input input = gc.getInput();
	        
	        
	        if(!terminado){
	            
	            if(input.isKeyDown(Input.KEY_LCONTROL)){
	                if(input.isKeyPressed(Input.KEY_Z)){
	                    Desfazer.undo(hero,bombs,places);
	                }
	            }
	
	            if(input.isKeyPressed(Input.KEY_ESCAPE)) {
	            	Game.pause();
	            	sbg.enterState(Game.MENUJOGOSTATE);
	            }
	            
	            if(input.isKeyPressed(Input.KEY_LEFT))
	            {
	                hero.goEsq(places, bombs, blocked, hero.getX(), hero.getY(), i);
	            }
	
	            if(input.isKeyPressed(Input.KEY_RIGHT))
	            {
	                hero.goDir(places, bombs, blocked, hero.getX(), hero.getY(), i); 
	            }
	
	            if(input.isKeyPressed(Input.KEY_UP))
	            {
	                hero.goCima(places, bombs, blocked, hero.getX(), hero.getY(), i);
	            }
	
	            if(input.isKeyPressed(Input.KEY_DOWN)) {
	                hero.goBaixo(places, bombs, blocked, hero.getX(), hero.getY(), i);
	            }
	            
	        }
        }//paused
    }
}
